package com.neuedu.controller.backend;

import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.Category;
import com.neuedu.pojo.Product;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "/mananger/product",description = "后台商品管理相关接口")
@Controller
@RequestMapping("/manager/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping(value = "addProduct",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> addProduct(@RequestParam(value = "name") String name,
                                       @RequestParam(value = "subtitle")String subtitle,
                                       @RequestParam(value = "categoryid")Integer categoryid,
                                       @RequestParam(value = "mainImage")MultipartFile mainFile,
                                       @RequestParam(value = "subImages") MultipartFile[] subImages,
                                       @RequestParam(value = "detail")MultipartFile detail,
                                       @RequestParam(value = "isNew")Integer isNew,
                                       @RequestParam(value = "isHot")Integer isHot,
                                       @RequestParam(value = "isBanner")Integer isBanner,
                                       HttpSession session){

        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            Product product = new Product();
            product.setName(name);
            product.setSubtitle(subtitle);
            product.setCategoryId(categoryid);
            product.setIsNew(isNew);
            product.setIsHot(isHot);
            product.setIsBanner(isBanner);
            product.setStatus(1);
            // 商品的添加
            JSONData<String> data = productService.addProduct(product, mainFile, subImages, detail);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }

    /**
     * 获取所有商品信息：==》分页
     */
    @ApiOperation(value = "获取所有商品信息",notes = "描述信息",httpMethod = "POST")
    @RequestMapping(value = "queryAllProduct",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<List<Product>> queryAllProduct(HttpSession session,
                                                   @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                   @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){

        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<List<Product>> data = productService.queryAllProduct(pageNum, pageSize);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "搜索相对应的商品信息",notes = "描述信息",httpMethod = "POST")
    @RequestMapping(value = "searchProduct",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<List<Product>> searchProduct(HttpSession session,
                                                 @RequestParam(value = "productId",required = false)String productId,
                                                 @RequestParam(value = "productName",required = false)String prodcutName,
                                                 @RequestParam(value = "categoryId",required = false)String categoryId,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<List<Product>> data = productService.searchProduct(productId, prodcutName, categoryId, pageNum, pageSize);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "修改商品状态信息",notes = "描述信息",httpMethod = "POST")
    @RequestMapping(value = "changeProductStatus",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> changeProductStatus(HttpSession session,
                                                @RequestParam(value = "productId")Integer productId,
                                                @RequestParam(value = "status")Integer status){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<String> data = productService.changeProductStatus(productId, status);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
}
